'''
Author: error: git config user.name && git config user.email & please set dead value or install git
Date: 2022-10-26 22:49:09
LastEditors: error: git config user.name && git config user.email & please set dead value or install git
LastEditTime: 2022-10-29 10:56:02
FilePath: \新建文件夹\Y分形树.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
#-*- coding:utf-8-*-
from tkinter import *;
import math;
masterWindow = Tk (); 
masterWindow.title ('分形绘制树的演示程序');
masterWindow.geometry ('1200x1200');

oneCanvas = Canvas ( masterWindow,width=1800,height=1800,bg="WHITE")
oneCanvas.pack();

finishLevel=10;
treePercent=0.7;
LeftInter_angle=30;
RightInter_angle=LeftInter_angle;
initPoint=[600,900];
initAngle=math.pi/2;
initLong=300;
initLevel=0;
LeftInter_angle=LeftInter_angle*math.pi/180;
RightInter_angle=RightInter_angle*math.pi/180;

def drawTree (startPoint,treeLong,treeAngle,treeLevel):
    
    
    leftPoint=[0,0];
    rightPoint=[0,0]; 

    treeLong=treeLong*treePercent;

    leftPoint[0]=startPoint[0]+treeLong*math.cos(treeAngle+LeftInter_angle);
    leftPoint[1]=startPoint[1]-treeLong*math.sin(treeAngle+LeftInter_angle);
    rightPoint[0]=startPoint[0]+treeLong*math.cos(treeAngle-RightInter_angle);
    rightPoint[1]=startPoint[1]-treeLong*math.sin(treeAngle-RightInter_angle);

    oneCanvas.create_line(startPoint[0],startPoint[1],leftPoint[0],leftPoint[1],fill="#476042")
    oneCanvas.create_line(startPoint[0],startPoint[1],rightPoint[0],rightPoint[1],fill="#476042")
    masterWindow.update();

    if(treeLevel>finishLevel):
        return;

    drawTree(leftPoint,treeLong,treeAngle+LeftInter_angle,treeLevel+1);
    drawTree(rightPoint,treeLong,treeAngle-RightInter_angle,treeLevel+1);


oneCanvas.create_line(initPoint[0],initPoint[1],initPoint[0],initPoint[1]+300,fill="#476042")
masterWindow.update();

drawTree(initPoint,initLong,initAngle,initLevel);
masterWindow.mainloop();









